package com.bt.lms.ui.action.school_education.stu_basic.familyMember;

import com.bt.lms.ui.page.school_education.stu_basic.Nation;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.List;

/**
 * 家庭成员的添加功能
 */
public class FamilyMemberAddAction implements EventHandler {

    private VBox vBox;

    private Integer baseWidth = 1200;

    public static ToggleGroup toggleGroup;

    private List list;

    private HBox familyHBox;

    public FamilyMemberAddAction(VBox vBox, List list,HBox totalHBox) {
        this.vBox = vBox;
        this.list = list;
        this.familyHBox = totalHBox;
    }

    public static ToggleGroup getToggleGroup() {
        return toggleGroup;
    }

    public static void setToggleGroup(ToggleGroup toggleGroup) {
        FamilyMemberAddAction.toggleGroup = toggleGroup;
    }

    @Override
    public void handle(Event event) {

        VBox totalBox = new VBox();

        HBox btnBox = new HBox();
        btnBox.setPrefWidth(1230);
        btnBox.setAlignment(Pos.CENTER_RIGHT);
        btnBox.setSpacing(10);
        Insets insets1 = new Insets(10,30,10,10);
        btnBox.setPadding(insets1);
        Button saveBtn = new Button("保存");
        Button cancelBtn = new Button("取消");
        btnBox.getChildren().add(saveBtn);
        btnBox.getChildren().add(cancelBtn);
        BtnStyle.saveBtn(saveBtn);
        BtnStyle.cancelBtn(cancelBtn);


        ObservableList<Node> children = this.vBox.getChildren();
        totalBox.getChildren().add(btnBox);

        //验证事件
        AddVerifyAction addVerifyAction = new AddVerifyAction(this.vBox,this.familyHBox,this.list);
        saveBtn.setOnAction(addVerifyAction);
        saveBtn.setCursor(Cursor.HAND);
        cancelBtn.setCursor(Cursor.HAND);

        //家庭成员信息的主盒子
        VBox totalHBox = new VBox();
        totalHBox.setPrefWidth(baseWidth);
        totalHBox.setSpacing(10);

        HBox one = new HBox();
        one.setSpacing(10);
        Label relationLab = new Label("关系");
        relationLab.setPrefWidth(baseWidth/8);
        TextField relationTF = new TextField();
        relationTF.setId("relation");
        relationTF.setPrefWidth(baseWidth/6);
        Label nameLab = new Label("成员姓名");
        nameLab.setPrefWidth(baseWidth/8);
        TextField nameTF = new TextField();
        nameTF.setId("name");
        nameTF.setPrefWidth(baseWidth/6);
        Label birthdayLab = new Label("出生年月");
        birthdayLab.setPrefWidth(baseWidth/8);
        DatePicker birthdayDP = new DatePicker();
        birthdayDP.setId("birthday");
        birthdayDP.setPrefWidth(baseWidth/6);
        one.getChildren().addAll(relationLab,relationTF,nameLab,nameTF,birthdayLab,birthdayDP);
        totalHBox.getChildren().add(one);

        HBox two = new HBox();
        two.setSpacing(10);
        Label workUnitLab = new Label("成员工作单位");
        workUnitLab.setPrefWidth(baseWidth/8);
        TextField workUnitTF = new TextField();
        workUnitTF.setId("workUnit");
        workUnitTF.setPrefWidth(baseWidth/6);
        Label jobLab = new Label("从业");
        jobLab.setPrefWidth(baseWidth/8);
        TextField jobTF = new TextField();
        jobTF.setId("job");
        jobTF.setPrefWidth(baseWidth/6);
        Label phoneLab = new Label("电话");
        phoneLab.setPrefWidth(baseWidth/8);
        TextField phoneTF = new TextField();
        phoneTF.setId("phone");
        phoneTF.setPrefWidth(baseWidth/6);
        two.getChildren().addAll(workUnitLab,workUnitTF,jobLab,jobTF,phoneLab,phoneTF);
        totalHBox.getChildren().add(two);

        HBox three = new HBox();
        three.setSpacing(10);
        Label nationLab = new Label("民族");
        nationLab.setPrefWidth(baseWidth/8);
        ComboBox nationTF = new ComboBox<>();
        nationTF.setId("nation");
        //调用方法
        Nation.nation(nationTF);
        nationTF.getSelectionModel().select(0);
        nationTF.setPrefWidth(baseWidth/6);
        Label nationalityLab = new Label("国籍/地区");
        nationalityLab.setPrefWidth(baseWidth/8);
        ComboBox nationalityTF = new ComboBox<>();
        nationalityTF.setId("nationality");
        nationalityTF.getItems().addAll("中国大陆","中国香港","中国台湾","中国澳门");
        nationalityTF.getSelectionModel().select(0);
        nationalityTF.setPrefWidth(baseWidth/6);
        Label healthConditionLab = new Label("健康状况");
        healthConditionLab.setPrefWidth(baseWidth/8);
        TextField healthConditionTF = new TextField();
        healthConditionTF.setId("healthCondition");
        healthConditionTF.setPrefWidth(baseWidth/6);
        three.getChildren().addAll(nationLab,nationTF,nationalityLab,nationalityTF,healthConditionLab,healthConditionTF);
        totalHBox.getChildren().add(three);


        HBox four = new HBox();
        four.setSpacing(10);
        Label dutyLab = new Label("专业技术职务");
        dutyLab.setPrefWidth(baseWidth/8);
        TextField dutyTF = new TextField();
        dutyTF.setId("duty");
        dutyTF.setPrefWidth(baseWidth/6);
        Label dutyRankLab = new Label("职务级别");
        dutyRankLab.setPrefWidth(baseWidth/8);
        TextField dutyRankTF = new TextField();
        dutyRankTF.setId("dutyRank");
        dutyRankTF.setPrefWidth(baseWidth/6);
        Label emailLab = new Label("电子邮箱");
        emailLab.setPrefWidth(baseWidth/8);
        TextField emailTF = new TextField();
        emailTF.setId("email");
        emailTF.setPrefWidth(baseWidth/6);
        four.getChildren().addAll(dutyLab,dutyTF,dutyRankLab,dutyRankTF,emailLab,emailTF);
        totalHBox.getChildren().add(four);

        Label isEmergencyContact = new Label("是否紧急联系人");
        isEmergencyContact.setPrefWidth(baseWidth / 8);
        HBox hBox2 = new HBox();
        hBox2.setSpacing(10);
        ToggleGroup toggleGroup = new ToggleGroup();
        RadioButton yes = new RadioButton("是");
        yes.setId("yesRDB");
        RadioButton no = new RadioButton("否");
        no.setId("noRDB");
        toggleGroup.getToggles().addAll(yes,no);
        hBox2.getChildren().addAll(isEmergencyContact,yes,no);
        totalHBox.getChildren().add(hBox2);
        totalBox.getChildren().add(totalHBox);

        children.add(totalBox);
        list.add(totalBox);

        /**
         * 取消按钮的匿名内部类
         */
        cancelBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                int size = list.size();

                children.remove(size);
                list.remove(size-1);

            }
        });


    }
}
